package com.polaris.live.popup.userprofile

import android.content.Context
import android.view.View
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupUserProfileAnchorBinding
import com.polaris.live.interface_bag.PAGViewListener
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.net.liveService
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.toUserBasicVo
import com.polaris.live.resp.go_resp.LiveFollowBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.photo.PicturePreview
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.widget.pag.AddFollowView
import org.libpag.PAGView

/**
 * 主播打开用户资料弹窗
 *
 * @author Created by 天晴 on 2024/4/8 11:03
 * @since 1.0.0
 **/
class AnchorUserProfilePopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val infoOtherResp: InfoOtherResp,
    private val mPage: Int,
    private val objectId: Long?,
    private val mBanMessageUser: MutableList<Long>?,
    private val onMessageClickListener: OnClickListener? = null,
    private val onFollowerClickListener: (() -> Unit)? = null,
    private val onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
) : BaseLiveBottomPopupView<PopupUserProfileAnchorBinding>(
    context,
    { PopupUserProfileAnchorBinding.bind(it) }) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_user_profile_anchor
    }

    private var isOpenBan = false //是否公屏禁言 false 没有
    override fun onCreate() {
        super.onCreate()
        mBinding = PopupUserProfileAnchorBinding.bind(popupImplView)

        isOpenBan = mBanMessageUser?.find { userId -> userId == infoOtherResp.userId } != null


        mBinding.apply {
            /**
             * 主播不是自己显示ban直播间消息
             */
            if (infoOtherResp.userId != UserManager.getUserId()) {
                if (infoOtherResp.relation?.followed == true) {
                    addFollower.visibility = View.GONE
                } else {
                    addFollower.visibility = View.VISIBLE
                }

            } else {
                icMoreCard.visibility = View.GONE
                goMessage.visibility = View.GONE
                addFollower.visibility = View.GONE
                llBottom.visibility = View.GONE
            }

            baseView.initView(infoOtherResp, mPage, true, {
                StartCallProxy.builder(this@AnchorUserProfilePopup, context).startCall(
                    mViewModel,
                    UserConst.From.LIVE,
                    UserConst.LastFrom.LIVE_DETAIL,
                    LiveConst.SenderFrom.MANUAL,
                    infoOtherResp.userId,
                    infoOtherResp.toUserBasicVo(),
                    {
                        dismiss()
                    })
            }, { data, view, position ->
                PicturePreview.with(activity)
                    .byView(view)
                    .setImageUrlList(data)
                    .setIndex(position)
                    .setUserInfo(infoOtherResp)
                    .go()
            }, {
            }, {
                if (LiveStateManager.isLiving() || LiveStateManager.isCalling()) {
                    return@initView
                }
                Router.instance.setPath(RoutePath.USER_SPACE_ACTIVITY)
                    .withInt("page", mPage)
                    .withAny("userBasic", infoOtherResp.toUserBasicVo())
                    .withLong("userId", infoOtherResp.userId).navigation()
            })
            addFollower.apply {
                setPathType(AddFollowView.LIVE_ADD_FOLLOWE_ALPHA)
                addListener(object : PAGViewListener {
                    override fun onAnimationEnd(p0: PAGView?) {
                        super.onAnimationEnd(p0)
                        addFollower.visibility = View.INVISIBLE
                    }
                })
                setRepeatCount(1)
            }
            if (infoOtherResp.relation?.followed == true) {
                addFollower.visibility = View.INVISIBLE
            }
            addFollower.setOnSingleClickListener {
                objectId?.let {
                    mViewModel.request(
                        { liveService.liveFollow(LiveFollowBean(it, infoOtherResp.userId)) },
                        {
                            onFollowerClickListener?.invoke()
                            mBinding.addFollower.play()
                            ToastGravity.showGravity(string.follower_succeed)
                        })
                }
            }
            goMessage.setOnSingleClickListener {
                if (onMessageClickListener == null) {
                    Router.instance.setPath(RoutePath.MESSAGE_ACTIVITY)
                        .withInt("page", mPage)
                        .withString("nickname", infoOtherResp.nickname)
                        .withString("avatar", infoOtherResp.avatar)
                        .apply {
                            infoOtherResp.decoration?.avatarFrame?.also { af ->
                                withString("avatarFrame", af)
                            }
                            infoOtherResp.decoration?.chatBubble?.also { cb ->
                                withString("chatBubble", cb)
                            }
                        }
                        .withLong("userId", infoOtherResp.userId)
                        .navigation()
                } else {
                    onMessageClickListener.onClick(it)
                }
                dismiss()
            }

            icMoreCard.setOnSingleClickListener {
                onSelectMoreCardListener?.invoke(infoOtherResp)
                dismiss()
            }
        }
    }

    override fun getContentView(): View = mBinding.bottom
}